@echo off
REM 创建开始菜单快捷方式

REM 设置字符编码
chcp 65001 > nul

setlocal

set appPath=%~1
set act=%~2
set type=%~3
set shortcutName=%~4

if "%type%"=="" (
    set type=script
)

REM 检查传入文件路径是否正确

if "%appPath%"=="" ( 
    echo 应用程序路径未设置
    exit /b 1
)

if not exist "%appPath%" (
	echo %appPath% 不存在，请输入正确路径
	exit /b 2
)

if "%shortcutName%"=="" (
    if "%appPath:~-4%" NEQ ".exe" (
        echo "非法应用，应用的扩展名是exe"
    )
    call :setAppName "%appPath%"
)


REM 所有用户，需要管理员权限
REM set editFileContextMenu=HKEY_CLASSES_ROOT\*\shell\Edit with %shortcutName%
REM set editFileContextMenuCommand=HKEY_CLASSES_ROOT\*\shell\Edit with %shortcutName%\command

REM 为当前用户，不需要管理员权限
set editFileContextMenu=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Edit with %shortcutName%
set editFileContextMenuCommand=HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Edit with %shortcutName%\command
reg query "%editFileContextMenu%" 1>nul 2>nul
if %errorlevel%==0 (
    reg delete "%editFileContextMenu%" /f 1>nul 2>nul
    rem echo -----------    应该能级联删除    ------------------------------------------------------
)

if "%act%"=="install" (
    REG ADD "%editFileContextMenu%" /ve /t REG_SZ /d "用%shortcutName%打开" 1>nul 2>nul
    REG ADD "%editFileContextMenu%" /v Icon /t REG_SZ /d "%appPath%,0" 1>nul 2>nul
    REG ADD "%editFileContextMenuCommand%" /ve /t REG_SZ /d "%appPath% \"%%1\"" 1>nul 2>nul
    
    if "%type%"=="script" (
        echo -----------    添加右键打开文件完成    ------------------------------------------------------
        pause
    )
)

if "%act%"=="uninstall" (
    REM reg delete "%editFileContextMenu%" /f 1>nul 2>nul
    if "%type%"=="script" (
        echo -----------    删除右键打开文件完成    ------------------------------------------------------
        pause
    )
    exit /b %errorlevel%
)
exit /b 99

:setAppName
SET shortcutName=%~n1
SET appName=%~nx1
exit /b